package com.tucc.hello.leetcode.no1_400.no1_200.no81_100.no87;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();

    }

    public boolean isScramble(String s1, String s2) {
        int len = s1.length();
        if (len == s2.length()) {
            return isScramble(s1, s2, 0, len - 1, 0, len - 1);
        }
        return false;
    }

    public boolean isScramble(String s1, String s2, int l1, int r1, int l2, int r2) {
        if (l1 >= r1) {
            return true;
        }
        if (s1.charAt(l1) == s2.charAt(l2)) {
            return isScramble(s1, s2, l1 + 1, r1, l2 + 1, r2);
        } else if (s1.charAt(r1) == s2.charAt(r2)) {
            return isScramble(s1, s2, l1, r1 - 1, l2, r2 - 1);
        } else if (s1.charAt(l1) == s2.charAt(r2)) {
            return isScramble(s1, s2, l1 + 1, r1, l2, r2 - 1);
        } else if (s1.charAt(r1) == s2.charAt(l2)) {
            return isScramble(s1, s2, l1, r1 - 1, l2 + 1, r2);
        }

        return false;
    }
}
